package androidcamp.socialfitnesstracker;

import androidcamp.socialfitnesstracker.data.*;

import java.util.ArrayList;
import java.util.List;

public class MockDB {

    private List<Goal> goals;
    private List<Workout> workouts;

    private List<ListListener<Goal>> goalListeners;
    private List<ListListener<Workout>> workoutListeners;
    
    public static MockDB mockDB;
    static {
        mockDB = new MockDB();
        mockDB.addMockupData();
    }

    public MockDB() {
        goals = new ArrayList<Goal>();
        workouts = new ArrayList<Workout>();
        goalListeners = new ArrayList<ListListener<Goal>>();
        workoutListeners = new ArrayList<ListListener<Workout>>();
    }

    public static MockDB get() {
        return mockDB;
    }

    public List<Goal> getAllGoals() {
        return goals;
    }
    
    public List<Workout> getWorkoutsForGoal(Goal goal) {
        List<Workout> res = new ArrayList<Workout>();
        for(Workout w: workouts) {
            if(w.getType().equals(goal.getType()))
                res.add(w);
        }
        return res;
    }

    public void insertNewWorkout(Workout workout) {
        workouts.add(workout);
        for (ListListener<Workout> workoutListener : workoutListeners) {
            workoutListener.itemAdded(workouts.size()-1, workout);
        }
    }
    
    public void insertNewGoal(Goal goal) {
        goals.add(goal);
        for (ListListener<Goal> goalListener: goalListeners) {
            goalListener.itemAdded(goals.size()-1, goal);
        }
    }

    public void addGoalListener(ListListener<Goal> listener) {
        goalListeners.add(listener);
    }

    public void addWorkoutListener(ListListener<Workout> listener) {
        workoutListeners.add(listener);
    }

    public void addMockupData() {
        goals.add(new Goal(WorkoutType.RUNNING, 5*60*60, 1, TimeUnit.WEEK));
        goals.add(new Goal(WorkoutType.SWIMMING, 1*60*60, 1, TimeUnit.DAY));
        goals.add(new Goal(WorkoutType.YOGA, 1*60*60, 2, TimeUnit.WEEK));
    }

}
